from pymongo import MongoClient
import time

def check_logged_in_users():
    # 连接到MongoDB
    client = MongoClient("mongodb://192.168.168.110:27017/")
    
    # 选择数据库
    db = client['data']

    while True:  # 无限循环
        # 查询group为1的用户
        users = db.user.find({"group": "1"})

        # 收集未登录用户的name
        not_logged_in_users = []
        for user in users:
            if not user.get("is_logged_in"):  # 如果is_logged_in为False
                not_logged_in_users.append(user.get("name"))

        # 输出所有未登录用户的name
        if not_logged_in_users:
            print("用户: " + ", ".join(not_logged_in_users),"未登录无法开始")
        else:
            print("所有用户均已登录,3秒后开始~~~")
            time.sleep(3) 
            break  # 如果所有用户均已登录，退出循环

        time.sleep(5)  # 暂停5秒

    client.close()

# 主程序
if __name__ == "__main__":
    check_logged_in_users()
